package com.xcms.test;


import com.xcms.test.mail.MailHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@WebServlet(name = "IdentifyingCodeServlet")
public class IdentifyingCodeServlet extends HttpServlet {
    private static final char[] CHARS={'2','3','4','5','6','7','8','9','A','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
            'S','T','U','V','W','X','Y','Z'};
    String host,from,pwd,to,subject,content;
    List<String> files=new ArrayList<>();
    public static Random random=new Random();
    public static String getRandomString(){
        StringBuffer buffer=new StringBuffer();
        for(int i=0;i<4;i++){
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        String randomString=getRandomString();
        System.out.println("正在执行获取验证码操作");
        host="smtp.yeah.net";
        from="tanzhzh@yeah.net";
        to="1440721309@qq.com";
        pwd="QGLELQBQFLEBPHGD";
        subject="验证码";
        content=randomString;
        //把当前验证码放到会话中
        request.getSession(true).setAttribute("identify",randomString);
        to=request.getParameter("email").toString();
        if(MailHelper.sendEmail(host,from,pwd,to,subject,content,files)){
            System.out.println("check");
            response.sendRedirect("Homework/zhuce.jsp");

        }
    }
}
